package cn.jly.boot.mp.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @PackageName cn.jly.boot.mp.utils
 * @ClassName SpringContextUtils
 * @Description spring的上下文工具
 * @Author 姬岚洋
 * @Date 2021/1/6 上午 9:51
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static <T> T getBean(Class<T> beanClazz) {
        return applicationContext.getBean(beanClazz);
    }

    public static <T> T getBean(String beanName, Class<T> beanClazz) {
        return applicationContext.getBean(beanName, beanClazz);
    }
}
